<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/9/24
 * Time: 19:19
 */

namespace app\admin\controller;


use app\admin\model\TeacherModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class TeacherController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $data = Db::name('teacher')
            ->where(function (Query $query) use ($name){
                if ($name){
                    $query ->where('name','like',"%$name%");
                }
            })
            ->order('id desc')
            ->paginate(3,false,$this->request->param());
        $data->appends(['name'=>$name]);
        $page = $data->render();
        return view('',['teacher'=>$data,'page'=>$page]);
    }
    public function add()
    {
        return view('');
    }
    public function addPost()
    {
        $data = input();
        if (empty($data['name'])){
            $this->error('老师名不能为空');
        }
        $add = TeacherModel::create($data);
        if ($add){
            $this->success('添加成功','teacher/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit($id)
    {
        $teacher=TeacherModel::get($id);
        return $this->fetch('',['teacher'=>$teacher]);
    }
    public function editPost()
    {
        $data = input();
        if (empty($data['name'])){
            $this->error('老师名不能为空');
        }
        $edit = TeacherModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','teacher/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete()
    {
        $id=input('id');
        $del=TeacherModel::destroy($id);
        if ($del){
            $this->success("删除成功！", url("teacher/index"));
        }else{
            $this->error("删除失败！");
        }
    }
}